package com.ymatou.iapi.godfreightmgr.testcase;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.shop2cn.iapi.shenqisettlement.parameter.resp.GetCurrencyConfigResp;
import com.ymatou.iapi.godfreightmgr.parameter.CreateFreightDetailDto;
import com.ymatou.iapi.godfreightmgr.parameter.CreateFreightDto;
import com.ymatou.iapi.godfreightmgr.parameter.CreateFreightTemplateBean;
import com.ymatou.iapi.godfreightmgr.parameter.FreightInfoVo;
import com.ymatou.iapi.godfreightmgr.parameter.GetTemplateInfoBean;
import com.ymatou.iapi.godfreightmgr.service.GetTemplateInfoCall;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AgentProductCallService;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.GodfreightmgrService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.freight_template;
import com.ymttest.database.model.freight_template_detail;
import com.ymttest.database.sqlwapper.godfreight.freight_templateWapper;
import com.ymttest.database.sqlwapper.godfreight.freight_template_detailWapper;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.MapUtil;

/**
*运费模板详情
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetTemplateInfo{
	private static GetTemplateInfoBean gettemplateinfoBean;
	private static GetTemplateInfoCall gettemplateinfoCall;
	private static freight_templateWapper templateWapper = new freight_templateWapper();
	private static freight_template_detailWapper detailWapper = new freight_template_detailWapper();
	private static int sellerId = AgentUsers.rootUserId;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("运费模板详情");
	}

	@Before
	public void caseUp() {
		gettemplateinfoBean=new GetTemplateInfoBean();
		gettemplateinfoCall=new GetTemplateInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(gettemplateinfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyDB(int templateId) throws IOException {
		String ret = gettemplateinfoCall.getJsonObject().get("data").getAsJsonObject().toString();
		FreightInfoVo tgtFreightInfoVo = (FreightInfoVo) FormatUtil.GSONToObject(ret, FreightInfoVo.class);
		freight_template template = templateWapper.selectByPrimaryKey(templateId);
		List<freight_template_detail> templateDetails = detailWapper.selectByTemplateId(templateId);

		FreightInfoVo actFreightInfoVo = new FreightInfoVo();
		actFreightInfoVo.setSellerId(template.getSellerId());
		actFreightInfoVo.setShareTemplate(template.getIsShareTemplate());
		actFreightInfoVo.setTemplateId(templateId);
		actFreightInfoVo.setTemplateName(template.getTemplateName());
		actFreightInfoVo.setCurrency(template.getCurrency());
		GetCurrencyConfigResp currencyConfigResp = AgentProductCallService.getCurrencyConfigResp(template.getCurrency());
		actFreightInfoVo.setCurrencyName(currencyConfigResp.getName());
		actFreightInfoVo.setCurrencySymbol(currencyConfigResp.getMark());

		List<freight_template_detail> mainFreightDetail = templateDetails.stream().filter(x -> x.getSaleMode() == 1 || x.getSaleMode() == 3).collect(Collectors.toList());
		List<freight_template_detail> agentFreightDetail = templateDetails.stream().filter(x -> x.getSaleMode() == 2).collect(Collectors.toList());

		CreateFreightDto mainFreight = new CreateFreightDto();
		mainFreight.setCalcModel(template.getDirectSaleCalcFeeMode());
		List<CreateFreightDetailDto> createFreightDetailDtos = mainFreightDetail.stream().map(x -> mapCreateFreightDetailDto(x)).collect(Collectors.toList());
		mainFreight.setTemplateItems(createFreightDetailDtos);
		if (!CollectionUtils.isEmpty(agentFreightDetail)){
			CreateFreightDto agentFreight = new CreateFreightDto();
			agentFreight.setCalcModel(template.getAgentSaleCalcFeeMode());
			List<CreateFreightDetailDto> createFreightDetailDtos1 = agentFreightDetail.stream().map(x -> mapCreateFreightDetailDto(x)).collect(Collectors.toList());
			agentFreight.setTemplateItems(createFreightDetailDtos1);
			actFreightInfoVo.setAgentFreight(agentFreight);
		}
		actFreightInfoVo.setMainFreight(mainFreight);

		Logger.debug("actFreightInfoVo:"+actFreightInfoVo);

		Map<String, Object> tgtMap = MapUtil.pojoToMap(tgtFreightInfoVo);
		tgtMap.remove("mainFreight");
		tgtMap.remove("agentFreight");
		Map<String, Object> actMap = MapUtil.pojoToMap(actFreightInfoVo);
		actMap.remove("mainFreight");
		actMap.remove("agentFreight");
		AssertService.assertResultEqual(tgtMap, actMap);

		CreateFreightDto tgtmainFreight = tgtFreightInfoVo.getMainFreight();
		CreateFreightDto actmainFreight = actFreightInfoVo.getMainFreight();
		Map<String, Object> tgtmainFreightMap = MapUtil.pojoToMap(tgtmainFreight);
		tgtmainFreightMap.remove("templateItems");
		Map<String, Object> actmainFreightMap = MapUtil.pojoToMap(actmainFreight);
		actmainFreightMap.remove("templateItems");
		AssertService.assertResultEqual(tgtmainFreightMap, actmainFreightMap);

		List<CreateFreightDetailDto> tgttemplateItems = tgtmainFreight.getTemplateItems();
		List<CreateFreightDetailDto> acttemplateItems = actmainFreight.getTemplateItems();
		tgttemplateItems.forEach(p->{
			CreateFreightDetailDto createFreightDetailDto = acttemplateItems.stream().filter(x -> x.getFirstFee().equals(p.getFirstFee())).findFirst().orElse(null);
			if (createFreightDetailDto!=null){
				Map<String, Object> map = MapUtil.pojoToMap(p);
				Map<String, Object> map1 = MapUtil.pojoToMap(createFreightDetailDto);
				AssertService.assertResultEqual(map, map1);
			}
		});

		if (tgtFreightInfoVo.getAgentFreight()!=null){
			CreateFreightDto tgtmainFreight1 = tgtFreightInfoVo.getAgentFreight();
			CreateFreightDto actmainFreight1 = actFreightInfoVo.getAgentFreight();
			Map<String, Object> tgtmainFreightMap1 = MapUtil.pojoToMap(tgtmainFreight1);
			tgtmainFreightMap1.remove("templateItems");
			Map<String, Object> actmainFreightMap1 = MapUtil.pojoToMap(actmainFreight1);
			actmainFreightMap.remove("templateItems");
			AssertService.assertResultEqual(tgtmainFreightMap1, actmainFreightMap1);

			List<CreateFreightDetailDto> tgttemplateItems1 = tgtmainFreight1.getTemplateItems();
			List<CreateFreightDetailDto> acttemplateItems1 = actmainFreight1.getTemplateItems();
			tgttemplateItems1.forEach(p->{
				CreateFreightDetailDto createFreightDetailDto = acttemplateItems1.stream().filter(x -> x.getFirstFee().equals(p.getFirstFee())).findFirst().orElse(null);
				if (createFreightDetailDto!=null){
					Map<String, Object> map = MapUtil.pojoToMap(p);
					Map<String, Object> map1 = MapUtil.pojoToMap(createFreightDetailDto);
					AssertService.assertResultEqual(map, map1);
				}
			});
		}

	}

	public CreateFreightDetailDto mapCreateFreightDetailDto(freight_template_detail templateDetail){
		CreateFreightDetailDto createFreightDetailDto = new CreateFreightDetailDto();
		createFreightDetailDto.setDefaultTemplate(templateDetail.getIsDefault());
		createFreightDetailDto.setDeliveryRegion(StringUtils.isEmpty(templateDetail) ? Arrays.asList() : Arrays.asList(templateDetail.getDeliveryCity().split(",")));
		createFreightDetailDto.setFirstFee(templateDetail.getFirstFee());
		createFreightDetailDto.setAddFee(templateDetail.getAddFee());
		if (templateDetail.getCalcFeeMode()==1){
			createFreightDetailDto.setFirstNum(templateDetail.getFirstNum().intValue());
			createFreightDetailDto.setAddNum(templateDetail.getAddNum().intValue());
		}else if (templateDetail.getCalcFeeMode()==2){
			createFreightDetailDto.setFirstWeight(templateDetail.getFirstNum());
			createFreightDetailDto.setAddWeight(templateDetail.getAddNum());
		}
		return createFreightDetailDto;
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTemplateInfo_01() {
		Logger.start(true,"获取主运费模板和代理运费详情，成功");
		try {
			createmchBean createmchBean = GodmchManageCallService.InitcreatemchBeanForChannel();
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			CreateFreightTemplateBean createFreightTemplateBean = GodfreightmgrService.initCreateFreightTemplate(createmchBean.getMchId(), 1, 1);
			String templateId = GodfreightmgrService.CreateFreightTemplate(createFreightTemplateBean);
			gettemplateinfoBean.setSellerId(createmchBean.getMchId());
			gettemplateinfoBean.setTemplateId(Integer.parseInt(templateId));
			gettemplateinfoCall.setData(gettemplateinfoBean);
			gettemplateinfoCall.callService();
			Logger.verifyEquals("200",gettemplateinfoCall.getString("code"),"验证code");
			verifyDB(Integer.parseInt(templateId));

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTemplateInfo_02() {
		Logger.start(true,"获取共享运费模板详情，成功");
		try {
			createmchBean createmchBean = GodmchManageCallService.InitcreatemchBeanForChannel();
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			CreateFreightTemplateBean createFreightTemplateBean = GodfreightmgrService.initCreateFreightTemplate(createmchBean.getMchId(), 1, 2);
			String templateId = GodfreightmgrService.CreateFreightTemplate(createFreightTemplateBean);
			gettemplateinfoBean.setSellerId(createmchBean.getMchId());
			gettemplateinfoBean.setTemplateId(Integer.parseInt(templateId));
			gettemplateinfoCall.setData(gettemplateinfoBean);
			gettemplateinfoCall.callService();
			Logger.verifyEquals("200",gettemplateinfoCall.getString("code"),"验证code");
			verifyDB(Integer.parseInt(templateId));
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTemplateInfo_03() {
		Logger.start(true,"获取共享运费模板详情，多模板，成功");
		try {
			createmchBean createmchBean = GodmchManageCallService.InitcreatemchBeanForChannel();
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			CreateFreightTemplateBean createFreightTemplateBean = GodfreightmgrService.initCreateFreightTemplate(createmchBean.getMchId(), 1, 2);

			CreateFreightDto createFreightDto = new CreateFreightDto();
			createFreightDto.setCalcModel(2);

			CreateFreightDetailDto createFreightDetail = new CreateFreightDetailDto();
			createFreightDetail.setDefaultTemplate(true);//是否默认运费模板
			createFreightDetail.setFirstWeight(new BigDecimal(1));//首件重量
			createFreightDetail.setAddWeight(new BigDecimal(2));//续件重量
			createFreightDetail.setFirstFee(new BigDecimal(15));//首件/首重运费
			createFreightDetail.setAddFee(new BigDecimal(10));//续件 / 续重 运费

			CreateFreightDetailDto createFreightDetail1 = new CreateFreightDetailDto();
			List<String> deliveryRegion = new ArrayList<>();
			deliveryRegion.add("北京市");
			createFreightDetail1.setDefaultTemplate(false);//是否默认运费模板
			createFreightDetail1.setDeliveryRegion(deliveryRegion);//配送区域，非默认运费模板必填
			createFreightDetail1.setFirstWeight(new BigDecimal(1));//首件重量
			createFreightDetail1.setAddWeight(new BigDecimal(2));//续件重量
			createFreightDetail1.setFirstFee(new BigDecimal(20));//首件/首重运费
			createFreightDetail1.setAddFee(new BigDecimal(10));//续件 / 续重 运费

			List<CreateFreightDetailDto> templateItems = new ArrayList<>();
			templateItems.add(createFreightDetail);
			templateItems.add(createFreightDetail1);
			createFreightDto.setTemplateItems(templateItems);//运费明细内容
			createFreightTemplateBean.setMainFreight(createFreightDto);

			String templateId = GodfreightmgrService.CreateFreightTemplate(createFreightTemplateBean);
			gettemplateinfoBean.setSellerId(createmchBean.getMchId());
			gettemplateinfoBean.setTemplateId(Integer.parseInt(templateId));
			gettemplateinfoCall.setData(gettemplateinfoBean);
			gettemplateinfoCall.callService();
			Logger.verifyEquals("200",gettemplateinfoCall.getString("code"),"验证code");
			verifyDB(Integer.parseInt(templateId));
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTemplateInfo_04() {
		Logger.start(true,"sellerId为空，获取失败");
		try {
			createmchBean createmchBean = GodmchManageCallService.InitcreatemchBeanForChannel();
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			CreateFreightTemplateBean createFreightTemplateBean = GodfreightmgrService.initCreateFreightTemplate(createmchBean.getMchId(), 1, 2);
			String templateId = GodfreightmgrService.CreateFreightTemplate(createFreightTemplateBean);
			gettemplateinfoBean.setTemplateId(Integer.parseInt(templateId));
			gettemplateinfoCall.setData(gettemplateinfoBean);
			gettemplateinfoCall.callService();
			Logger.verifyEquals("400",gettemplateinfoCall.getString("code"),"验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTemplateInfo_05() {
		Logger.start(true,"templateId为空，获取失败");
		try {
			createmchBean createmchBean = GodmchManageCallService.InitcreatemchBeanForChannel();
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			CreateFreightTemplateBean createFreightTemplateBean = GodfreightmgrService.initCreateFreightTemplate(createmchBean.getMchId(), 1, 2);
			String templateId = GodfreightmgrService.CreateFreightTemplate(createFreightTemplateBean);
			gettemplateinfoBean.setSellerId(createmchBean.getMchId());
			gettemplateinfoCall.setData(gettemplateinfoBean);
			gettemplateinfoCall.callService();
			Logger.verifyEquals("400",gettemplateinfoCall.getString("code"),"验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}